using System.Web.Mvc;
using good1.ApplicationServices.Dto;
using good1.ApplicationServices.Interface;
using good1.Core;
using good1.Core.Enums;
using good1.Web.Controllers.Attributes;
using good1.Web.Controllers.Infrastructure;
using good1.Web.Controllers.Infrastructure.Cookies;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace good1.Web.Controllers
{
    [HandleError]
    [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
    public class PhotosController : BaseController
    {
        public PhotosController(IRepository<Photo> photoRepository, ISecurityContextService securityContext) : base(securityContext)
        {
            Check.Require(photoRepository != null, "photoRepository may not be null");

            this.photoRepository = photoRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Photo> photos = photoRepository.GetAll();
            return View(photos);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Photo photo = photoRepository.Get(id);
            return View(photo);
        }

        public ActionResult Create() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Photo photo) {
            if (photo.IsValid()) {
                photoRepository.SaveOrUpdate(photo);

                TempData["message"] = "The photo was successfully created.";
                return RedirectToAction("Index");
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState,
                photo.ValidationResults());
            return View();
        }

        [Transaction]
        public ActionResult Edit(int id) {
            Photo photo = photoRepository.Get(id);
            return View(photo);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [ModelBinder(typeof(DefaultModelBinder))] Photo photo) {
            Photo photoToUpdate = photoRepository.Get(id);
            TransferFormValuesTo(photoToUpdate, photo);

            if (photoToUpdate.IsValid()) {
                TempData["message"] = "The photo was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                photoRepository.DbContext.RollbackTransaction();
                MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, 
                    photoToUpdate.ValidationResults());
                return View(photoToUpdate);
            }
        }

        private void TransferFormValuesTo(Photo photoToUpdate, Photo photoFromForm) {
			photoToUpdate.Name = photoFromForm.Name;
			photoToUpdate.Url = photoFromForm.Url;
			photoToUpdate.Description = photoFromForm.Description;
			photoToUpdate.TagLine = photoFromForm.TagLine;
            photoToUpdate.IsThumbnail = photoFromForm.IsThumbnail;
            photoToUpdate.Project = photoFromForm.Project ;
         }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The photo was successfully deleted.";
            Photo photoToDelete = photoRepository.Get(id);

            if (photoToDelete != null) {
                photoRepository.Delete(photoToDelete);

                try {
                    photoRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the photo from being deleted. " +
						"Another item likely depends on this photo.";
                    photoRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The photo could not be found for deletion. It may already have been deleted.";
            }

            TempData["Message"] = resultMessage;
            return RedirectToAction("Index");
        }

        private readonly IRepository<Photo> photoRepository;

    }
}
